<?php

class WebUser extends CWebUser {
    const PUBLISHER_ROLE_ID=1;
    const READER_ROLE_ID=2;
    const PUBLISHER_ATTR_ID='id';
    const READER_ATTR_ID='id';


    public $loginUrl = array('/user/user/login');

    /**
     * Performs access check for this user.
     * @param string the name of the role that need access check.
     * @param array name-value pairs that would be passed to business rules associated
     * with the tasks and roles assigned to the user.
     * @param boolean whether to allow caching the result of access checki.
     * This parameter has been available since version 1.0.5. When this parameter
     * is true (default), if the access check of an operation was performed before,
     * its result will be directly returned when calling this method to check the same operation.
     * If this parameter is false, this method will always call {@link CAuthManager::checkAccess}
     * to obtain the up-to-date access result. Note that this caching is effective
     * only within the same request.
     * @return boolean whether the operations can be performed by this user.
     */
    public function checkAccess($operation, $params=array(), $allowCaching=true) {
        return $this->hasRole($operation);
    }

    /**
     * Checks if this (non-admin) User can administrate some users
     */
    public static function hasUsers($uid = 0) {
        if ($uid == 0)
            $uid = Yii::app()->user->getId();

        $user = CActiveRecord::model('User')->findByPk($uid);

        return isset($user->users);
    }

    /**
     * Checks if this (non-admin) User can administrate the given user
     */
    public static function hasUser($username, $uid = 0) {

        if ($uid == 0)
            $uid = Yii::app()->user->getId();

        // Every user can modify himself
        if ($username == $uid)
            return true;

        $user = CActiveRecord::model('User')->findByPk($uid);

        if (!is_array($username))
            $username = array($username);

        if (isset($user->users))
            foreach ($user->users as $userobj) {
                if (in_array($userobj->username, $username) ||
                        in_array($userobj->id, $username))
                    return true;
            }
        return false;
    }

    /**
     * Checks if the user has the given Role
     * @mixed Role string or array of strings that should be checked
     * @int (optional) id of the user that should be checked 
     * @return bool Return value tells if the User has access or hasn't access.
     */
//	public static function hasRole($role, $uid = 0)
//	{
//		if($uid == 0)
//			$uid = Yii::app()->user->getId();
//
//		if(!is_array($role))
//			$role = array ($role);
//
//	//	$user = CActiveRecord::model('User')->findByPk($uid);
//                 $user = CActiveRecord::model('AqUsers')->findByPk($uid);
//		if(isset($user->roles)) 
//			foreach($user->roles as $roleobj) 
//			{
//				if(in_array($roleobj->title, $role) ||
//				  in_array($roleobj->id, $role))
//					return true;
//			}
//		return false;
//	}
    public static function hasRole($role, $uid = 0) {
        if ($uid == 0)
            $uid = Yii::app()->user->getId();


        if (!is_array($role))
            $role = array($role);

        $user = CActiveRecord::model('User')->findByPk($uid);

        if (isset($user->roles))
            foreach ($user->roles as $roleobj) {
                if (in_array($roleobj->title, $role) ||
                        in_array($roleobj->id, $role))
                    return true;
            }
        return false;
    }

    /**
     * Return admin status.
     * @return boolean
     */
    public function isAdmin() {
        if ($this->isGuest)
            return false;
        else {


            if (User::model()->active()->superuser()->findbyPk(Yii::app()->user->id))
                return true;
            else
                return false;
        }
    }

    public function afterLogin($fromCookie) {
        $this->setUsersIdInSession();
        parent::afterLogin($fromCookie);
    }

    public function setUsersIdInSession() {
        $session = new CHttpSession;
        $model = $this->getLoginUser();
        if ($model) {
            $session->add($attribute, $model->getAttribute($attribute));
        }
    }

    public function getLoginUser() {
        $model = false;
        $uid = Yii::app()->getUser()->getId();
        if (Yii::app()->getUser()->hasRole(self::PUBLISHER_ROLE_ID)) {
            $attribute = self::PUBLISHER_ATTR_ID;
            $model = Users::model()->find('id=:id', array(':id' => $uid));
        } elseif (Yii::app()->getUser()->hasRole(self::READER_ROLE_ID)) {
            $attribute = self::READER_ROLE_ID;
            $model = Users::model()->find('id=:id', array(':id' => $uid));
        }
        return $model;
    }
    
    public function getPublisher() {
        $publisher=false;
        $model = $this->getLoginUser();
        if (Yii::app()->getUser()->hasRole(self::PUBLISHER_ROLE_ID)) {
           $publisher= current($model->publishers);
        }
        return $publisher;
    }

}

?>
